
import tkinter as tk
from tkinter import ttk, messagebox, scrolledtext
from crud import create_patient, read_patients, update_patient, delete_patient
from crud import create_appointment, read_appointments, update_appointment, delete_appointment
from crud import create_medical_record, read_medical_records, update_medical_record, delete_medical_record
from crud import create_prescription, read_prescriptions, update_prescription, delete_prescription
from crud import create_invoice, read_invoices, update_invoice, delete_invoice
from queries import list_appointments_with_details, calculate_revenue_per_department, find_busy_doctors

class HospitalGUI:
    def __init__(self, root):
        self.root = root
        self.root.title("Hospital Management System")
        self.root.geometry("1000x700")
        self.root.configure(bg="#e3f2fd")  # Light blue background

        menubar = tk.Menu(self.root)
        file_menu = tk.Menu(menubar, tearoff=0)
        file_menu.add_command(label="Exit", command=self.root.quit)
        menubar.add_cascade(label="File", menu=file_menu)

        entity_menu = tk.Menu(menubar, tearoff=0)
        entity_menu.add_command(label="Patients", command=self.show_patients)
        entity_menu.add_command(label="Appointments", command=self.show_appointments)
        entity_menu.add_command(label="Medical Records", command=self.show_medical_records)
        entity_menu.add_command(label="Prescriptions", command=self.show_prescriptions)
        entity_menu.add_command(label="Invoices", command=self.show_invoices)
        menubar.add_cascade(label="Entities", menu=entity_menu)

        query_menu = tk.Menu(menubar, tearoff=0)
        query_menu.add_command(label="Appointments with Details", command=self.show_query1)
        query_menu.add_command(label="Revenue per Department", command=self.show_query2)
        query_menu.add_command(label="Busy Doctors", command=self.show_query3)
        menubar.add_cascade(label="Queries", menu=query_menu)

        self.root.config(menu=menubar)

        self.main_frame = tk.Frame(self.root, bg="#e3f2fd")
        self.main_frame.pack(fill=tk.BOTH, expand=True)
        self.current_tree = None

    def clear_frame(self):
        for widget in self.main_frame.winfo_children():
            widget.destroy()
        self.current_tree = None

    def refresh_table(self):
        if self.current_tree:
            for item in self.current_tree.get_children():
                self.current_tree.delete(item)
            data = self.get_current_data()
            for row in data:
                self.current_tree.insert("", "end", values=row)

    def get_current_data(self):
        if hasattr(self, 'current_entity'):
            if self.current_entity == "patients":
                return read_patients()
            elif self.current_entity == "appointments":
                return read_appointments()
            elif self.current_entity == "medical_records":
                return read_medical_records()
            elif self.current_entity == "prescriptions":
                return read_prescriptions()
            elif self.current_entity == "invoices":
                return read_invoices()
            elif self.current_entity == "query1":
                return list_appointments_with_details()
            elif self.current_entity == "query2":
                return calculate_revenue_per_department()
            elif self.current_entity == "query3":
                return find_busy_doctors(5)
        return []

    def display_table(self, data):
        if not data:
            tk.Label(self.main_frame, text="No data found", font=("Arial", 12), fg="#d32f2f", bg="#e3f2fd").pack(pady=10)
            return

        # Create a frame for the treeview and scrollbars
        table_frame = tk.Frame(self.main_frame, bg="#e3f2fd")
        table_frame.pack(fill=tk.BOTH, expand=True, pady=10)

        # Vertical scrollbar
        v_scroll = tk.Scrollbar(table_frame, orient="vertical")
        v_scroll.pack(side=tk.RIGHT, fill=tk.Y)

        # Horizontal scrollbar
        h_scroll = tk.Scrollbar(table_frame, orient="horizontal")
        h_scroll.pack(side=tk.BOTTOM, fill=tk.X)

        # Treeview with scrollbars
        self.current_tree = ttk.Treeview(table_frame, columns=[str(i) for i in range(len(data[0]))], show="headings", yscrollcommand=v_scroll.set, xscrollcommand=h_scroll.set)
        v_scroll.config(command=self.current_tree.yview)
        h_scroll.config(command=self.current_tree.xview)

        # Style the Treeview
        style = ttk.Style()
        style.configure("Treeview", background="#bbdefb", foreground="#0d47a1", fieldbackground="#bbdefb", font=("Arial", 10))
        style.configure("Treeview.Heading", background="#1976d2", foreground="white", font=("Arial", 12, "bold"))

        for i, col in enumerate(data[0]):
            self.current_tree.heading(str(i), text=str(col))
            self.current_tree.column(str(i), width=150, anchor="center")  # Adjust column width as needed

        for row in data:
            self.current_tree.insert("", "end", values=row)

        self.current_tree.pack(fill=tk.BOTH, expand=True)

    def show_patients(self):
        self.clear_frame()
        self.current_entity = "patients"
        tk.Label(self.main_frame, text="Patients Management", font=("Arial", 16, "bold"), fg="#1976d2", bg="#e3f2fd").pack(pady=10)

        # Search frame
        search_frame = tk.Frame(self.main_frame, bg="#e3f2fd")
        search_frame.pack(pady=10)
        tk.Label(search_frame, text="Search by Name:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(side=tk.LEFT, padx=5)
        search_entry = tk.Entry(search_frame, font=("Arial", 12))
        search_entry.pack(side=tk.LEFT, padx=5)
        tk.Button(search_frame, text="Search", command=lambda: self.search_patients(search_entry.get()), font=("Arial", 12), bg="#2196f3", fg="white").pack(side=tk.LEFT, padx=5)

        # Display table
        data = read_patients()
        self.display_table(data)

        # Buttons frame
        buttons_frame = tk.Frame(self.main_frame, bg="#e3f2fd")
        buttons_frame.pack(pady=10)
        tk.Button(buttons_frame, text="Create Patient", command=self.create_patient_form, font=("Arial", 12), bg="#4caf50", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Update Patient", command=self.update_patient_form, font=("Arial", 12), bg="#ff9800", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Delete Patient", command=self.delete_patient_form, font=("Arial", 12), bg="#f44336", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Refresh", command=self.refresh_table, font=("Arial", 12), bg="#2196f3", fg="white").pack(side=tk.LEFT, padx=10)

    def search_patients(self, search_name):
        data = read_patients(search_name)
        for item in self.current_tree.get_children():
            self.current_tree.delete(item)
        for row in data:
            self.current_tree.insert("", "end", values=row)

    def show_appointments(self):
        self.clear_frame()
        self.current_entity = "appointments"
        tk.Label(self.main_frame, text="Appointments Management", font=("Arial", 16, "bold"), fg="#1976d2", bg="#e3f2fd").pack(pady=10)

        # Display table
        data = read_appointments()
        self.display_table(data)

        # Buttons frame
        buttons_frame = tk.Frame(self.main_frame, bg="#e3f2fd")
        buttons_frame.pack(pady=10)
        tk.Button(buttons_frame, text="Create Appointment", command=self.create_appointment_form, font=("Arial", 12), bg="#4caf50", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Update Appointment", command=self.update_appointment_form, font=("Arial", 12), bg="#ff9800", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Delete Appointment", command=self.delete_appointment_form, font=("Arial", 12), bg="#f44336", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Refresh", command=self.refresh_table, font=("Arial", 12), bg="#2196f3", fg="white").pack(side=tk.LEFT, padx=10)

    def show_medical_records(self):
        self.clear_frame()
        self.current_entity = "medical_records"
        tk.Label(self.main_frame, text="Medical Records Management", font=("Arial", 16, "bold"), fg="#1976d2", bg="#e3f2fd").pack(pady=10)

        # Display table
        data = read_medical_records()
        self.display_table(data)

        # Buttons frame
        buttons_frame = tk.Frame(self.main_frame, bg="#e3f2fd")
        buttons_frame.pack(pady=10)
        tk.Button(buttons_frame, text="Create Medical Record", command=self.create_medical_record_form, font=("Arial", 12), bg="#4caf50", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Update Medical Record", command=self.update_medical_record_form, font=("Arial", 12), bg="#ff9800", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Delete Medical Record", command=self.delete_medical_record_form, font=("Arial", 12), bg="#f44336", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Refresh", command=self.refresh_table, font=("Arial", 12), bg="#2196f3", fg="white").pack(side=tk.LEFT, padx=10)

    def show_prescriptions(self):
        self.clear_frame()
        self.current_entity = "prescriptions"
        tk.Label(self.main_frame, text="Prescriptions Management", font=("Arial", 16, "bold"), fg="#1976d2", bg="#e3f2fd").pack(pady=10)

        # Display table
        data = read_prescriptions()
        self.display_table(data)

        # Buttons frame
        buttons_frame = tk.Frame(self.main_frame, bg="#e3f2fd")
        buttons_frame.pack(pady=10)
        tk.Button(buttons_frame, text="Create Prescription", command=self.create_prescription_form, font=("Arial", 12), bg="#4caf50", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Update Prescription", command=self.update_prescription_form, font=("Arial", 12), bg="#ff9800", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Delete Prescription", command=self.delete_prescription_form, font=("Arial", 12), bg="#f44336", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Refresh", command=self.refresh_table, font=("Arial", 12), bg="#2196f3", fg="white").pack(side=tk.LEFT, padx=10)

    def show_invoices(self):
        self.clear_frame()
        self.current_entity = "invoices"
        tk.Label(self.main_frame, text="Invoices Management", font=("Arial", 16, "bold"), fg="#1976d2", bg="#e3f2fd").pack(pady=10)

        # Display table
        data = read_invoices()
        self.display_table(data)

        # Buttons frame
        buttons_frame = tk.Frame(self.main_frame, bg="#e3f2fd")
        buttons_frame.pack(pady=10)
        tk.Button(buttons_frame, text="Create Invoice", command=self.create_invoice_form, font=("Arial", 12), bg="#4caf50", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Update Invoice", command=self.update_invoice_form, font=("Arial", 12), bg="#ff9800", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Delete Invoice", command=self.delete_invoice_form, font=("Arial", 12), bg="#f44336", fg="white").pack(side=tk.LEFT, padx=10)
        tk.Button(buttons_frame, text="Refresh", command=self.refresh_table, font=("Arial", 12), bg="#2196f3", fg="white").pack(side=tk.LEFT, padx=10)

    def show_query1(self):
        self.clear_frame()
        self.current_entity = "query1"
        tk.Label(self.main_frame, text="Appointments with Details", font=("Arial", 16, "bold"), fg="#1976d2", bg="#e3f2fd").pack(pady=10)
        self.display_table(list_appointments_with_details())
        tk.Button(self.main_frame, text="Refresh", command=self.refresh_table, font=("Arial", 12), bg="#2196f3", fg="white").pack(pady=10)

    def show_query2(self):
        self.clear_frame()
        self.current_entity = "query2"
        tk.Label(self.main_frame, text="Revenue per Department", font=("Arial", 16, "bold"), fg="#1976d2", bg="#e3f2fd").pack(pady=10)
        self.display_table(calculate_revenue_per_department())
        tk.Button(self.main_frame, text="Refresh", command=self.refresh_table, font=("Arial", 12), bg="#2196f3", fg="white").pack(pady=10)

    def show_query3(self):
        self.clear_frame()
        self.current_entity = "query3"
        tk.Label(self.main_frame, text="Busy Doctors (>5 appointments this month)", font=("Arial", 16, "bold"), fg="#1976d2", bg="#e3f2fd").pack(pady=10)
        self.display_table(find_busy_doctors(5))
        tk.Button(self.main_frame, text="Refresh", command=self.refresh_table, font=("Arial", 12), bg="#2196f3", fg="white").pack(pady=10)

    def create_patient_form(self):
        form = tk.Toplevel(self.root)
        form.title("Create Patient")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Name:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        name_entry = tk.Entry(form, font=("Arial", 12))
        name_entry.pack(pady=5)
        tk.Label(form, text="DOB (YYYY-MM-DD):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        dob_entry = tk.Entry(form, font=("Arial", 12))
        dob_entry.pack(pady=5)
        tk.Label(form, text="Contact:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        contact_entry = tk.Entry(form, font=("Arial", 12))
        contact_entry.pack(pady=5)
        tk.Label(form, text="Email:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        email_entry = tk.Entry(form, font=("Arial", 12))
        email_entry.pack(pady=5)
        tk.Label(form, text="Medical History:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        history_entry = scrolledtext.ScrolledText(form, font=("Arial", 12), height=5)
        history_entry.pack(pady=5)
        tk.Button(form, text="Submit", command=lambda: self.submit_create_patient(
            name_entry.get(), dob_entry.get(), contact_entry.get(), email_entry.get(), history_entry.get("1.0", tk.END)
        ), font=("Arial", 12), bg="#4caf50", fg="white").pack(pady=10)

    def submit_create_patient(self, name, dob, contact, email, medical_history):
        try:
            create_patient(name, dob, contact, email, medical_history)
            messagebox.showinfo("Success", "Patient created")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def update_patient_form(self):
        form = tk.Toplevel(self.root)
        form.title("Update Patient")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Patient ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        id_entry = tk.Entry(form, font=("Arial", 12))
        id_entry.pack(pady=5)
        tk.Label(form, text="New Name (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        name_entry = tk.Entry(form, font=("Arial", 12))
        name_entry.pack(pady=5)
        tk.Label(form, text="New DOB (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        dob_entry = tk.Entry(form, font=("Arial", 12))
        dob_entry.pack(pady=5)
        tk.Label(form, text="New Contact (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        contact_entry = tk.Entry(form, font=("Arial", 12))
        contact_entry.pack(pady=5)
        tk.Label(form, text="New Email (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        email_entry = tk.Entry(form, font=("Arial", 12))
        email_entry.pack(pady=5)
        tk.Label(form, text="New Medical History (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        history_entry = scrolledtext.ScrolledText(form, font=("Arial", 12), height=5)
        history_entry.pack(pady=5)
        tk.Button(form, text="Update", command=lambda: self.submit_update_patient(
            int(id_entry.get()), name_entry.get(), dob_entry.get(), contact_entry.get(), email_entry.get(), history_entry.get("1.0", tk.END)
        ), font=("Arial", 12), bg="#ff9800", fg="white").pack(pady=10)

    def submit_update_patient(self, patient_id, name, dob, contact, email, medical_history):
        try:
            update_patient(patient_id, name or None, dob or None, contact or None, email or None, medical_history or None)
            messagebox.showinfo("Success", "Patient updated")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def delete_patient_form(self):
        form = tk.Toplevel(self.root)
        form.title("Delete Patient")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Patient ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        id_entry = tk.Entry(form, font=("Arial", 12))
        id_entry.pack(pady=5)
        tk.Button(form, text="Delete", command=lambda: self.submit_delete_patient(int(id_entry.get())), 
                  font=("Arial", 12), bg="#f44336", fg="white").pack(pady=10)

    def submit_delete_patient(self, patient_id):
        try:
            delete_patient(patient_id)
            messagebox.showinfo("Success", "Patient deleted")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def create_appointment_form(self):
        form = tk.Toplevel(self.root)
        form.title("Create Appointment")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Patient ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        patient_id_entry = tk.Entry(form, font=("Arial", 12))
        patient_id_entry.pack(pady=5)
        tk.Label(form, text="Staff ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        staff_id_entry = tk.Entry(form, font=("Arial", 12))
        staff_id_entry.pack(pady=5)
        tk.Label(form, text="Room ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        room_id_entry = tk.Entry(form, font=("Arial", 12))
        room_id_entry.pack(pady=5)
        tk.Label(form, text="Date (YYYY-MM-DD):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        date_entry = tk.Entry(form, font=("Arial", 12))
        date_entry.pack(pady=5)
        tk.Label(form, text="Time (HH:MM:SS):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        time_entry = tk.Entry(form, font=("Arial", 12))
        time_entry.pack(pady=5)
        tk.Button(form, text="Submit", command=lambda: self.submit_create_appointment(
            int(patient_id_entry.get()), int(staff_id_entry.get()), int(room_id_entry.get()), date_entry.get(), time_entry.get()
        ), font=("Arial", 12), bg="#4caf50", fg="white").pack(pady=10)

    def submit_create_appointment(self, patient_id, staff_id, room_id, date, time):
        try:
            create_appointment(patient_id, staff_id, room_id, date, time)
            messagebox.showinfo("Success", "Appointment created")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def update_appointment_form(self):
        form = tk.Toplevel(self.root)
        form.title("Update Appointment")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Appointment ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        id_entry = tk.Entry(form, font=("Arial", 12))
        id_entry.pack(pady=5)
        tk.Label(form, text="New Date (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        date_entry = tk.Entry(form, font=("Arial", 12))
        date_entry.pack(pady=5)
        tk.Label(form, text="New Time (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        time_entry = tk.Entry(form, font=("Arial", 12))
        time_entry.pack(pady=5)
        tk.Button(form, text="Update", command=lambda: self.submit_update_appointment(
            int(id_entry.get()), date_entry.get() or None, time_entry.get() or None
        ), font=("Arial", 12), bg="#ff9800", fg="white").pack(pady=10)

    def submit_update_appointment(self, appointment_id, date, time):
        try:
            update_appointment(appointment_id, date, time)
            messagebox.showinfo("Success", "Appointment updated")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def delete_appointment_form(self):
        form = tk.Toplevel(self.root)
        form.title("Delete Appointment")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Appointment ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        id_entry = tk.Entry(form, font=("Arial", 12))
        id_entry.pack(pady=5)
        tk.Button(form, text="Delete", command=lambda: self.submit_delete_appointment(int(id_entry.get())), 
                  font=("Arial", 12), bg="#f44336", fg="white").pack(pady=10)

    def submit_delete_appointment(self, appointment_id):
        try:
            delete_appointment(appointment_id)
            messagebox.showinfo("Success", "Appointment deleted")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def create_medical_record_form(self):
        form = tk.Toplevel(self.root)
        form.title("Create Medical Record")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Appointment ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        app_id_entry = tk.Entry(form, font=("Arial", 12))
        app_id_entry.pack(pady=5)
        tk.Label(form, text="Diagnosis:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        diagnosis_entry = tk.Entry(form, font=("Arial", 12))
        diagnosis_entry.pack(pady=5)
        tk.Label(form, text="Symptoms:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        symptoms_entry = scrolledtext.ScrolledText(form, font=("Arial", 12), height=3)
        symptoms_entry.pack(pady=5)
        tk.Label(form, text="Observations:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        observations_entry = scrolledtext.ScrolledText(form, font=("Arial", 12), height=3)
        observations_entry.pack(pady=5)
        tk.Button(form, text="Submit", command=lambda: self.submit_create_medical_record(
            int(app_id_entry.get()), diagnosis_entry.get(), symptoms_entry.get("1.0", tk.END), observations_entry.get("1.0", tk.END)
        ), font=("Arial", 12), bg="#4caf50", fg="white").pack(pady=10)

    def submit_create_medical_record(self, appointment_id, diagnosis, symptoms, observations):
        try:
            create_medical_record(appointment_id, diagnosis, symptoms, observations)
            messagebox.showinfo("Success", "Medical Record created")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def update_medical_record_form(self):
        form = tk.Toplevel(self.root)
        form.title("Update Medical Record")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Record ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        id_entry = tk.Entry(form, font=("Arial", 12))
        id_entry.pack(pady=5)
        tk.Label(form, text="New Diagnosis (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        diagnosis_entry = tk.Entry(form, font=("Arial", 12))
        diagnosis_entry.pack(pady=5)
        tk.Label(form, text="New Symptoms (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        symptoms_entry = scrolledtext.ScrolledText(form, font=("Arial", 12), height=3)
        symptoms_entry.pack(pady=5)
        tk.Label(form, text="New Observations (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        observations_entry = scrolledtext.ScrolledText(form, font=("Arial", 12), height=3)
        observations_entry.pack(pady=5)
        tk.Button(form, text="Update", command=lambda: self.submit_update_medical_record(
            int(id_entry.get()), diagnosis_entry.get() or None, symptoms_entry.get("1.0", tk.END) or None, observations_entry.get("1.0", tk.END) or None
        ), font=("Arial", 12), bg="#ff9800", fg="white").pack(pady=10)

    def submit_update_medical_record(self, record_id, diagnosis, symptoms, observations):
        try:
            update_medical_record(record_id, diagnosis, symptoms, observations)
            messagebox.showinfo("Success", "Medical Record updated")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def delete_medical_record_form(self):
        form = tk.Toplevel(self.root)
        form.title("Delete Medical Record")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Record ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        id_entry = tk.Entry(form, font=("Arial", 12))
        id_entry.pack(pady=5)
        tk.Button(form, text="Delete", command=lambda: self.submit_delete_medical_record(int(id_entry.get())), 
                  font=("Arial", 12), bg="#f44336", fg="white").pack(pady=10)

    def submit_delete_medical_record(self, record_id):
        try:
            delete_medical_record(record_id)
            messagebox.showinfo("Success", "Medical Record deleted")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def create_prescription_form(self):
        form = tk.Toplevel(self.root)
        form.title("Create Prescription")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Appointment ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        app_id_entry = tk.Entry(form, font=("Arial", 12))
        app_id_entry.pack(pady=5)
        tk.Label(form, text="Notes:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        notes_entry = scrolledtext.ScrolledText(form, font=("Arial", 12), height=5)
        notes_entry.pack(pady=5)
        tk.Button(form, text="Submit", command=lambda: self.submit_create_prescription(
            int(app_id_entry.get()), notes_entry.get("1.0", tk.END)
        ), font=("Arial", 12), bg="#4caf50", fg="white").pack(pady=10)

    def submit_create_prescription(self, appointment_id, notes):
        try:
            create_prescription(appointment_id, notes)
            messagebox.showinfo("Success", "Prescription created")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def update_prescription_form(self):
        form = tk.Toplevel(self.root)
        form.title("Update Prescription")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Prescription ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        id_entry = tk.Entry(form, font=("Arial", 12))
        id_entry.pack(pady=5)
        tk.Label(form, text="New Notes (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        notes_entry = scrolledtext.ScrolledText(form, font=("Arial", 12), height=5)
        notes_entry.pack(pady=5)
        tk.Button(form, text="Update", command=lambda: self.submit_update_prescription(
            int(id_entry.get()), notes_entry.get("1.0", tk.END) or None
        ), font=("Arial", 12), bg="#ff9800", fg="white").pack(pady=10)

    def submit_update_prescription(self, prescription_id, notes):
        try:
            update_prescription(prescription_id, notes)
            messagebox.showinfo("Success", "Prescription updated")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def delete_prescription_form(self):
        form = tk.Toplevel(self.root)
        form.title("Delete Prescription")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Prescription ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        id_entry = tk.Entry(form, font=("Arial", 12))
        id_entry.pack(pady=5)
        tk.Button(form, text="Delete", command=lambda: self.submit_delete_prescription(int(id_entry.get())), 
                  font=("Arial", 12), bg="#f44336", fg="white").pack(pady=10)

    def submit_delete_prescription(self, prescription_id):
        try:
            delete_prescription(prescription_id)
            messagebox.showinfo("Success", "Prescription deleted")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def create_invoice_form(self):
        form = tk.Toplevel(self.root)
        form.title("Create Invoice")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Appointment ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        app_id_entry = tk.Entry(form, font=("Arial", 12))
        app_id_entry.pack(pady=5)
        tk.Label(form, text="Amount:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        amount_entry = tk.Entry(form, font=("Arial", 12))
        amount_entry.pack(pady=5)
        tk.Button(form, text="Submit", command=lambda: self.submit_create_invoice(
            int(app_id_entry.get()), float(amount_entry.get())
        ), font=("Arial", 12), bg="#4caf50", fg="white").pack(pady=10)

    def submit_create_invoice(self, appointment_id, amount):
        try:
            create_invoice(appointment_id, amount)
            messagebox.showinfo("Success", "Invoice created")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def update_invoice_form(self):
        form = tk.Toplevel(self.root)
        form.title("Update Invoice")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Invoice ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        id_entry = tk.Entry(form, font=("Arial", 12))
        id_entry.pack(pady=5)
        tk.Label(form, text="New Amount (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        amount_entry = tk.Entry(form, font=("Arial", 12))
        amount_entry.pack(pady=5)
        tk.Label(form, text="New Status (optional):", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        status_entry = tk.Entry(form, font=("Arial", 12))
        status_entry.pack(pady=5)
        tk.Button(form, text="Update", command=lambda: self.submit_update_invoice(
            int(id_entry.get()), float(amount_entry.get()) if amount_entry.get() else None, status_entry.get() or None
        ), font=("Arial", 12), bg="#ff9800", fg="white").pack(pady=10)

    def submit_update_invoice(self, invoice_id, amount, status):
        try:
            update_invoice(invoice_id, amount, status)
            messagebox.showinfo("Success", "Invoice updated")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

    def delete_invoice_form(self):
        form = tk.Toplevel(self.root)
        form.title("Delete Invoice")
        form.configure(bg="#e3f2fd")
        tk.Label(form, text="Invoice ID:", font=("Arial", 12), fg="#424242", bg="#e3f2fd").pack(pady=5)
        id_entry = tk.Entry(form, font=("Arial", 12))
        id_entry.pack(pady=5)
        tk.Button(form, text="Delete", command=lambda: self.submit_delete_invoice(int(id_entry.get())), 
                  font=("Arial", 12), bg="#f44336", fg="white").pack(pady=10)

    def submit_delete_invoice(self, invoice_id):
        try:
            delete_invoice(invoice_id)
            messagebox.showinfo("Success", "Invoice deleted")
            self.refresh_table()
        except ValueError as e:
            messagebox.showerror("Error", str(e))

if __name__ == "__main__":
    root = tk.Tk()
    app = HospitalGUI(root)
    root.mainloop()
